package com.szy.douyou_article.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Article;
import com.szy.douyou_article.dao.State;
import com.szy.douyou_article.dao.req.ArticleReq;
import com.szy.douyou_article.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author sunziyan
 * @date 2022/3/1
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 分页查询帖子
     *
     * @param articleReq
     * @return
     */
    @PostMapping("/queryPage")
    public ResultDTO<Page<Article>> queryPage(@RequestBody ArticleReq articleReq) {

        return articleService.queryAllArticle(articleReq);

    }

    /**
     * 查询一条帖子
     *
     * @param articleId
     * @return
     */
    @GetMapping("/queryOne/{articleId}")
    public ResultDTO<Article> queryOne(@PathVariable("articleId") Long articleId) {

        return articleService.queryOneArticle(articleId);
    }

//    /**
//     * 编辑或添加一条数据
//     *
//     * @param article
//     * @return
//     */
//    @RequiresAuthentication
//    @PostMapping("/editOrAdd")
//    public ResultDTO editOrAdd(@Validated @RequestBody Article article) {
//        Subject subject = SecurityUtils.getSubject();
//        AccountProfile principal = (AccountProfile) subject.getPrincipal();
//        if (article.getArticleId() != null) {
//            ResultDTO<Article> result = articleService.queryOneArticle(article.getArticleId());
//            if (result.getSuccess() == false) {
//                return result;
//            }
//            Long userId = result.getData().getUserId();
//            //如果是不是该帖子的作者，不允许编辑
//            if (userId != principal.getUserId().longValue()) {
//                //如果是系统管理员，允许编辑
//                if (principal.getUserRight() == 1) {
//                    return articleService.updateOneArticle(article);
//                }
//                return ResultDTO.fail("没有编辑权限");
//            }
//            return articleService.updateOneArticle(article);
//
//        } else {
////            article.setUserId(principal.getUserId());
//            return articleService.addOneArticle(article);
//        }
//    }

    @PostMapping("/updateOne")
    public ResultDTO updateOne(@RequestBody Article article) {

        return articleService.updateOneArticle(article);
    }

    @PostMapping("/addOne")
    public ResultDTO addOne(@RequestBody Article article){

        return articleService.addOneArticle(article);
    }



    @GetMapping("/deleteOne/{articleId}")
    public ResultDTO deleteOne(@PathVariable("articleId") Long articleId) {

        return articleService.deleteOneArticle(articleId);
    }

    @GetMapping("/addArticleViews/{articleId}")
    public ResultDTO addArticleViews(@PathVariable("articleId") Long articleId) {

        return articleService.addArticleViews(articleId);
    }


    @GetMapping("/getAllState/{articleId}")
    public ResultDTO getAllState(@PathVariable("articleId") Long articleId) {
        return articleService.getArticleAllState(articleId);
    }

    @GetMapping("/getMyState/{articleId}/{userId}")
    public ResultDTO getMyState(@PathVariable("articleId") Long articleId, @PathVariable("userId") Long userId) {
        return articleService.getMyArticleState(articleId, userId);
    }


    @PostMapping("/createState")
    public ResultDTO createState(@RequestBody State state) {
        return articleService.createArticleState(state);
    }


    @PostMapping("/changeState")
    public ResultDTO changeState(@RequestBody State state) {
        return articleService.changeArticleState(state);
    }


}
